﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class UserRolesRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<UserRole> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.UserRoles
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p=>p.UserRoleID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public UserRole GetByID(int ID)
        {
            var result = from p in data.UserRoles
                         where p.UserRoleID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public IEnumerable<UserRole> GetByUserID(int ID)
        {
            var results = from p in data.UserRoles
                         where p.User.UserID == ID
                         select p;
            return results;
        }

        public IEnumerable<UserRole> GetByUser(string UserName)
        {
            return data.UserRoles.Where(u => u.User.UserName == UserName);
        }

        //Kiểm tra quyền đối với user
        public bool CheckRoles(User user, int RoleID)
        {
            if (user == null) return false;
            if(data.UserRoles.Any(p=>p.User.UserID == user.UserID && p.Role.RoleID == (int)Utility.Enum.Roles.Administrators))
                return true;
            return data.UserRoles.Any(p => p.User.UserID == user.UserID && p.Role.RoleID == RoleID);            
        }

        public bool CheckRoleforUser(int userID, int RoleID)
        {
            return data.UserRoles.Any(p => p.User.UserID == userID && p.Role.RoleID == RoleID);            
        }

        public IEnumerable<FunctionRole> GetByRoleID(int ID)
        {
            var results = from p in data.FunctionRoles
                         where p.Role.RoleID == ID
                         select p;
            return results;
        }

        public void Insert(FunctionRole ur)
        {
            data.AddToFunctionRoles(ur);
        }

        public void Insert(int userId, int roleID, int createby)
        {
            UserRole objRL = new UserRole
            {
                Role = data.Roles.FirstOrDefault(p => p.RoleID == roleID),
                User = data.Users.FirstOrDefault(p => p.UserID == userId),
                CreatedBy = createby,
                CreatedDate = DateTime.Now,
            };
            data.SaveChanges();
        }

        public void DeletebyUserandRole(int userId, int roleID)
        {
            var result = data.UserRoles.FirstOrDefault(p => p.User.UserID == userId && p.Role.RoleID == roleID);
            if (result != null)
            {
                data.DeleteObject(result);
            }
            data.SaveChanges();
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}